using System;
using System.Collections.Generic;
using System.Linq;
using Script.Data;
using UnityEngine;

namespace Script.UI.Panel
{
    public class LocationAnimatorCtrl : MonoBehaviour
    {
        public Dictionary<string, GameObject> animatorObjects;
        public static LocationAnimatorCtrl Ins;
        private GameObject cur;

        private void Awake()
        {
            Ins = this;
            animatorObjects = new Dictionary<string, GameObject>();
            var childCount = transform.childCount;
            for (var i = 0; i < childCount; i++)
            {
                var child = transform.GetChild(i);
                animatorObjects.Add(child.name, child.gameObject);
                // Debug.LogError(child.name);
                child.gameObject.SetActive(false);
            }
        }

        public void OnChange(int locationId)
        {
            Debug.LogWarning("点击" + locationId );
            if (cur)
            {
                cur.gameObject.SetActive(false);
                cur = null;
            }

            if (animatorObjects.ContainsKey(locationId.ToString()))
            {
                cur = animatorObjects[locationId.ToString()];
            }

            var conf = PositionConf.GetConfById(locationId.ToString());
            if (!cur)
            {
                foreach (var v in animatorObjects)
                {
                    if (v.Key.Contains(conf.code.ToString()))
                    {
                        cur = v.Value;
                        break;
                    }
                }
            }

            Debug.LogWarning("找到: " + cur == null );
            if (cur != null)
            {
                Debug.LogWarning("当前objnamee:" + cur.gameObject.name);
                Debug.LogWarning("动画是否在播放:" + cur.GetComponent<Animation>().isPlaying);
            }
            if (cur)
            {
                cur.gameObject.SetActive(true);
            }
   
        }
    }
}